package queue

import "fmt"

/**
顺序队列，
tail:指向下一个节点，head指向第一个节点，
循环插入
*/

const SIZE int = 20

type SeqQueue struct {
	elemQueue [SIZE]int
	head      int
	tail      int
}

func NewSeqQueue() *SeqQueue {
	return &SeqQueue{
	}
}

func (sq *SeqQueue) Push(value int) {
	if sq.getSize() == SIZE {
		fmt.Printf("queue is full")
		return
	}

	sq.elemQueue[sq.tail] = value
	sq.tail = (sq.tail + 1) % SIZE
}

func (sq *SeqQueue) Pop() int {
	if sq.getSize() == 0 {
		return -1
	}

	var result int
	result = sq.elemQueue[sq.head]
	sq.head = (sq.head + 1) % SIZE
	return result
}

func (sq *SeqQueue) Print() {
	tmpIndex := sq.head
	for ; tmpIndex != sq.tail; {
		fmt.Printf("%d ", sq.elemQueue[tmpIndex])
		tmpIndex = (tmpIndex + 1) % SIZE
	}
	fmt.Println()
}

func (sq *SeqQueue) getSize() int {
	return (sq.tail + SIZE - sq.head) % SIZE
}

func (sq *SeqQueue) isFull() bool {
	return sq.getSize() == SIZE
}

func (sq *SeqQueue) isEmpty() bool {
	return sq.getSize() == 0
}
